package com.lwk.springboot.dao.impl;

import com.lwk.springboot.dao.UserDao;
import com.lwk.springboot.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Auther: leiwenkang
 * @Date: 2020/8/29 20:53
 * @Description:
 */
@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;  //这个是系统自带的
    @Override
    public User getUserById(Integer id) {
        List<User> list = jdbcTemplate.query("select * from user_info where id = ?", new Object[]{id}, new BeanPropertyRowMapper(User.class));
        if(list!=null && list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }

    @Override
    public List<User> getUserList() {
        List<User> list = jdbcTemplate.query("select * from user_info", new Object[]{}, new BeanPropertyRowMapper(User.class));
        if(list!=null && list.size()>0){
            return list;
        }else{
            return null;
        }
    }

    @Override
    public int insert(User user) {
        return jdbcTemplate.update("insert into user_info(user_name, age, create_time, update_time) values(?, ?, ?, ?)",
                user.getUserName(),user.getAge(), user.getCreateTime(), user.getUpdateTime());
    }

    @Override
    public int update(Integer id, User user) {
        return jdbcTemplate.update("UPDATE user_info SET user_name = ? , age = ? , update_time = ? WHERE id=?",
                user.getUserName(),user.getAge(), id);
    }

    @Override
    public int delete(Integer id) {
        return jdbcTemplate.update("DELETE from user_info where id = ? ",id);
    }
}